<?php
    class Blog extends FrontEnd_Controller{
        function Blog(){
            parent::FrontEnd_Controller();
            $this->load->model('Blog_m');
        }

        function list_blog(){
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/blog/list_blog/' . '/' . $per_page . '/';
            $nav_link = $this->session->set_userdata('smart_navigate_url',  site_url('blog/list_blog')) ;
            $config['per_page'] = '5';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->db->count_all('seating_blog');
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }

            $this->_data['blogs']= $this->Blog_m->get_all_blogs($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->db->count_all('seating_blog');
            $this->_data['page_title'] = "Liste de blog";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('blog/list_blog');

        }
     
        function add_blog(){
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'blog_name',
                'Label' => 'Blog name',
                'rules' => 'required|callback_is_name_existe|xss_clean'
                ),
                array(
                'field' => 'blog_description',
                'Label' => 'Description name',
                'rules' => 'required|callback_is_name_existe|xss_clean'
                )
                );
                $this->form_validation->set_rules($formVal);
                if($this->form_validation->run()==TRUE){
                    $image_path = './assets/img_blog';
                    $thumb_path = $image_path . '/thumbs';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name'=>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);
                    if($this->upload->do_upload()){
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                       
                        $data_blog=array(
                        'name'=>$this->input->post('blog_name'),
                        'description'=>$this->input->post('blog_description'),
                        'blog_date'=>time(),
                        'image'=>$upload_info['file_name'],
                        'user_id'=>$this->session->userdata('user_id')
                        );
                        $this->Blog_m->add_blog($data_blog);
                        redirect('blog/list_blog');
                    }
                    else{
                        $this->display_view('blog/add_blog');
                    }
                }
                else{
                    $this->display_view('blog/add_blog');
                }
            }
            else{
                $this->display_view('blog/add_blog');
            }
        }
        function add_comment($blog_id){
            if($this->input->post('submit')){
                $formVal = array(
                array(
                'field' => 'comment',
                'Label' => 'Comment',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal);
                if($this->form_validation->run()==TRUE){
                    $data_comment=array(
                    'content'=>$this->input->post('comment'),
                    'comment_date'=>time(),
                    'blog_id'=>$blog_id,
                    'user_id'=>$this->session->userdata('user_id')
                    );
                    $this->Blog_m->add_comment($data_comment);
                    $this->detail_blog($blog_id);
                }
                else{
                    $this->detail_blog($blog_id);
                }
            }
            else{
                $this->detail_blog($blog_id);
            }
        }
        function detail_blog($blog_id)
        {
            $per_page = $this->uri->segment(4);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(5);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/blog/detail_blog/' .$blog_id. '/' . $per_page . '/';
            $nav_link = $this->session->set_userdata('smart_navigate_url',  site_url('blog/detail_blog'.'/'.$blog_id)) ;
            $config['per_page'] = '5';
            $config['uri_segment'] = 5;
            $config['total_rows'] = $this->Blog_m->count_comment($blog_id);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            
            $this->_data['comments'] = $this->Blog_m->get_comment_by_blog_id($blog_id,$config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Blog_m->count_comment($blog_id);
            $this->_data['page_title'] = "Liste de comment";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->_data['detail_blog']=$this->Blog_m->get_blog_by_id($blog_id);
            $this->display_view('blog/detail_blog');

        }
        function update_blog($blog_id){
              $this->_data['detail_blog']=$this->Blog_m->get_blog_by_id($blog_id);
              if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'blog_name',
                'Label' => 'Blog name',
                'rules' => 'required|callback_is_name_existe|xss_clean'
                ),
                array(
                'field' => 'blog_description',
                'Label' => 'Description name',
                'rules' => 'required|callback_is_name_existe|xss_clean'
                )
                );
                $this->form_validation->set_rules($formVal);
                if($this->form_validation->run()==TRUE){
                    $image_path = './assets/img_blog';
                    $thumb_path = $image_path . '/thumbs';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name'=>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);
                    if($this->upload->do_upload()){
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                       
                        $data_blog=array(
                        'name'=>$this->input->post('blog_name'),
                        'description'=>$this->input->post('blog_description'),
                        'blog_date'=>time(),
                        'image'=>$upload_info['file_name'],
                        'user_id'=>$this->session->userdata('user_id')
                        );
                        $this->Blog_m->update_blog($data_blog,$blog_id);
                        redirect('blog/list_blog');
                    }
                    else{
                         
                        $data_blog=array(
                        'name'=>$this->input->post('blog_name'),
                        'description'=>$this->input->post('blog_description'),
                        'blog_date'=>time(),
                        'user_id'=>$this->session->userdata('user_id')
                        );
                        $this->Blog_m->update_blog($data_blog,$blog_id);
                        redirect('blog/list_blog');
                    }
                }
                else{
                    $this->display_view('blog/update_blog');
                }
            }
            else{
                $this->display_view('blog/update_blog');
            }
        }
    }
?>
